package cn._2dland.comment.locator
{
	import cn._2dland.comment.CommentMC;
	import cn._2dland.utils.CommentUtils;
	
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * 自定义模式定位器
	 * @author deadblue
	 */
	public class CustomLocator extends CommentLocator
	{
		public function CustomLocator() {
			super();
		}

		override public function locate(cmnt:CommentMC, rect:Rectangle):void {
			// 计算横向速度
			var pt1:Point = getStartPoint(cmnt.angle, cmnt.rect, rect);
			var pt2:Point = getEndPoint(cmnt.angle, cmnt.rect, rect);

			if(isNaN(cmnt.x1)) cmnt.x1 = pt1.x;
			if(isNaN(cmnt.y1)) cmnt.x1 = pt1.y;
			if(isNaN(cmnt.x2)) cmnt.x2 = pt2.x;
			if(isNaN(cmnt.y2)) cmnt.y2 = pt2.y;

			cmnt.x = cmnt.x1;
			cmnt.y = cmnt.y1;
			cmnt.xSpeed = (cmnt.x2 - cmnt.x1) / cmnt.life;
			cmnt.ySpeed = (cmnt.y2 - cmnt.y1) / cmnt.life;

			_comments.push(cmnt);
		}

		private function getStartPoint(angle:int, cRect:Rectangle, vRect:Rectangle):Point {
			var pt:Point = new Point();

			switch(angle){
				case CommentUtils.ANGLE_NONE:
					break;
				case CommentUtils.ANGLE_LEFT_TOP:
					pt.x = vRect.width;
					pt.y = vRect.height;
					break;
				case CommentUtils.ANGLE_CENTER_TOP:
					pt.x = (vRect.width - cRect.width) / 2;
					pt.y = vRect.height;
					break;
				case CommentUtils.ANGLE_RIGHT_TOP:
					pt.x = 0 - cRect.width;
					pt.y = vRect.height;
					break;
				case CommentUtils.ANGLE_LEFT_MIDDLE:
					pt.x = vRect.width;
					pt.y = (vRect.height - cRect.height) / 2;
					break;
				case CommentUtils.ANGLE_CENTER_MIDDLE:
					pt.x = (vRect.width - cRect.width) / 2;
					pt.y = (vRect.height - cRect.height) / 2;
					break;
				case CommentUtils.ANGLE_RIGHT_MIDDLE:
					pt.x = 0 - cRect.width;
					pt.y = (vRect.height - cRect.height) / 2;
					break;
				case CommentUtils.ANGLE_LEFT_BOTTOM:
					pt.x = vRect.width;
					pt.y = 0 - cRect.height;
					break;
				case CommentUtils.ANGLE_CENTER_BOTTOM:
					pt.x = (vRect.width - cRect.width) / 2;
					pt.y = 0 - cRect.height;
					break;
				case CommentUtils.ANGLE_RIGHT_BOTTOM:
					pt.x = 0 - cRect.width;
					pt.y = 0 - cRect.height;
					break;
			}
			return pt;
		}
		
		private function getEndPoint(angle:int, cRect:Rectangle, vRect:Rectangle):Point {
			var pt:Point = new Point();
			switch(angle){
				case CommentUtils.ANGLE_NONE:
					break;
				case CommentUtils.ANGLE_LEFT_TOP:
					pt.x = 0 - cRect.width;
					pt.y = 0 - cRect.height;
					break;
				case CommentUtils.ANGLE_CENTER_TOP:
					pt.x = (vRect.width - cRect.width) / 2;
					pt.y = 0 - cRect.height;
					break;
				case CommentUtils.ANGLE_RIGHT_TOP:
					pt.x = vRect.width;
					pt.y = 0 - cRect.height;
					break;
				case CommentUtils.ANGLE_LEFT_MIDDLE:
					pt.x = 0 - cRect.width;
					pt.y = (vRect.height - cRect.height) / 2;
					break;
				case CommentUtils.ANGLE_CENTER_MIDDLE:
					pt.x = (vRect.width - cRect.width) / 2;
					pt.y = (vRect.height - cRect.height) / 2;
					break;
				case CommentUtils.ANGLE_RIGHT_MIDDLE:
					pt.x = vRect.width;
					pt.y = (vRect.height - cRect.height) / 2;
					break;
				case CommentUtils.ANGLE_LEFT_BOTTOM:
					pt.x = 0 - cRect.width;
					pt.y = vRect.height;
					break;
				case CommentUtils.ANGLE_CENTER_BOTTOM:
					pt.x = (vRect.width - cRect.width) / 2;
					pt.y = vRect.height;
					break;
				case CommentUtils.ANGLE_RIGHT_BOTTOM:
					pt.x = vRect.width;
					pt.y = vRect.height;
					break;
			}
			return pt;
		}
	}
}